package com.bank.service;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bank.code.RespCode;
import com.bank.dao.TellerDao;
import com.bank.entity.Teller;
import com.bank.excption.CommonException;
import com.bank.util.CommonUtil;
import com.bank.util.SessionUtils;
@Service("loginService")
public class MainService  extends BaseService{
	
	
	Logger logger= Logger.getLogger(MainService.class);
	
	@Autowired
	private TellerDao tellerDao;

	public void check(String number, String password,HttpServletRequest request) throws CommonException{
		
		Teller teller = tellerDao.getTellerByNumber(number);
		if(teller == null){
			throw new CommonException(RespCode.USER_NOT_EXIST, "用户名不存在");
		}
		
		String passwordM = CommonUtil.passwordEncryption(password);
		logger.debug("密码检查");
		logger.debug(passwordM);
		logger.debug(teller.getTeller_password());
		if(passwordM.equals(teller.getTeller_password())==false){
			throw new CommonException(RespCode.PASSWORD_NOT_CORRECT, "密码不正确");
		}else{
			
			SessionUtils.setSessionUserName(request, teller.getTeller_name());;
			SessionUtils.setSessionUserNumber(request, teller.getTeller_number());
			SessionUtils.setSessionUserOri(request, teller.getOrganization());
			teller.setTeller_status("1"); //设置登录状态，1已登录
			tellerDao.updateTellerInfo(teller);
			logger.debug("登录成功");
			this.setSesssionWorkdate(request);
		}
	}
	
	public String  logout(HttpServletRequest request){
		String number = (String) SessionUtils.getSessionUserNumber(request);
		Teller teller = tellerDao.getTellerByNumber(number);
		teller.setTeller_status("0");
		tellerDao.updateTellerInfo(teller);
		SessionUtils.removeSessionUserName(request);
		SessionUtils.removeSessionUserNumber(request);
		SessionUtils.removeSessionWokedate(request);
		SessionUtils.removeSessoionUserOri(request);
		SessionUtils.removeSessionUserNumber(request);
		return  "index";
	}

		
}
